/**
 * Created by Administrator on 2017/4/26.
 */
"use strict";

zmvp
    .controller("ChatController",function($scope,$rootScope,$http,$timeout,$cookieStore){
        $scope.GenNonDuplicateId = function(){
            var idStr = Date.now().toString(36);
            idStr += Math.random().toString(36).substr(3);
            return idStr;
        };

        $cookieStore.put("_passager",$scope.GenNonDuplicateId);

        //chat
        $scope.chatList=[];

        $scope.input="";
        $scope.submit=function(){
            $scope.chat={
                "time":new Date(),
                "type":0,
                "msg":$scope.input,
                "img":"assets/img/chat/headshot2.jpg",
                "name":"聪明的你"
            };
            $scope.chatList.push($scope.chat);
            $scope.tuLingJson={
                "key":"07d7f2aa8c0c4e66b4ee18c33ef4ae1b",
                "info":$scope.input,
                "loc":"北京市门头沟区"
                //"userid":$rootScope.presentAccountInfo.id
            };

            // $http({
            //     method:"POST",
            //     url:"http://www.tuling123.com/openapi/api",
            //     dateType:"jsonp",
            //     data:$scope.toLingJson,
            //     headers:{"Access-Control-Allow-Origin":'www.tuling123.com'}
            // }).success(function(req){
            //     console.log(req);
            // })
            $http({
                method:"GET",
                url:"http://www.tuling123.com/openapi/api?key=07d7f2aa8c0c4e66b4ee18c33ef4ae1b&info="+$scope.input+"&userid="+$cookieStore.get("_passager")
                // url:"http://www.tuling123.com/openapi/api?key=07d7f2aa8c0c4e66b4ee18c33ef4ae1b&info="+$scope.input
            }).success(function(req){
                $scope.input="";
                console.log(req);
                $scope.chat={
                    "time":new Date(),
                    "type":1,
                    "msg":req.text,
                    "link":req.url,
                    "img":"assets/img/chat/headshot1.jpg",
                    "name":"可爱的小Q",
                    "list":req.list
                };
                $scope.chatList.push($scope.chat);
                $timeout(function(){
                    $scope.refresh();
                },100);
            })
        };
        $scope.press=function(str){
            if(event.keyCode==13){
                $scope.submit();
            }
        };
        $scope.openLink=function(str){
            javascript:window.open(str);
        };
        $scope.refresh=function(){
            var div = document.getElementById("chat-window");
            div.scrollTop = div.scrollHeight;
        }

    });
